/*
 * Copyright 2013 - learnNcode (learnncode@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.mibao.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import com.learnncode.mediachooser.MediaChooser;
import com.learnncode.mediachooser.activity.HomeAddPicFragmentActivity;
import com.mibao.adapter.GalleryAdapter;
import com.mipai.R;

import java.util.ArrayList;
import java.util.List;

public class GalleryActivity extends Activity {

    GridView gridView;
    GalleryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_layout);
        gridView = (GridView)findViewById(R.id.gridView);

        IntentFilter imageIntentFilter = new IntentFilter(MediaChooser.IMAGE_SELECTED_ACTION_FROM_MEDIA_CHOOSER);
        registerReceiver(imageBroadcastReceiver, imageIntentFilter);

        initTitle();
    }

    private void initTitle(){
        ((ImageView)findViewById(R.id.head_right_img)).setImageResource(R.drawable.gallery_add);
        findViewById(R.id.head_right).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //MediaChooser.showCameraVideoView(false);
                //MediaChooser.showOnlyImageTab();
                Intent intent = new Intent(GalleryActivity.this, HomeAddPicFragmentActivity.class);
                startActivity(intent);
            }
        });
    }

    BroadcastReceiver imageBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ArrayList<String> s = intent.getStringArrayListExtra("list");
            Log.d(GalleryActivity.class.getSimpleName(), "list:" + intent.getStringArrayListExtra("list").toString());
            setAdapter(intent.getStringArrayListExtra("list"));
        }
    };

    @Override
    protected void onDestroy() {
        unregisterReceiver(imageBroadcastReceiver);
        super.onDestroy();
    }

    private void setAdapter( List<String> filePathList) {
        if(adapter == null){
            adapter = new GalleryAdapter(GalleryActivity.this, 0, filePathList);
            gridView.setAdapter(adapter);
        }else{
            adapter.addAll(filePathList);
            adapter.notifyDataSetChanged();
        }
    }
}
